package com.health.mapp.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.health.db.model.TrainingBase;
import com.health.db.model.TrainingBaseExample;
import com.health.db.service.user.ITrainingBaseService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.QueryTrainingBaseReq;
import com.health.mapp.bean.QueryTrainingBaseResp;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/***
 * 查询培训基本表
 * @author YC
 *
 */
public class QueryTrainingBaseImpl  implements ActionInterface{

	@Autowired
	ITrainingBaseService trainingBaseService;
	
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		
		QueryTrainingBaseReq req = (QueryTrainingBaseReq) reqBean;
		QueryTrainingBaseResp _resp = new QueryTrainingBaseResp();
		
		TrainingBaseExample example = new TrainingBaseExample();
		
		if (StringUtils.isNotEmpty(req.getId())) {
			example.setId(Long.valueOf(req.getId()));
			
		}else if (StringUtils.isNotEmpty(req.getName())) {
			example.setTnName(req.getName());
		}
		
		Integer pageNo=req.getPageNo();
		Integer pageSize=req.getPageNum();
		//判断页码
		if(null!=pageNo&&null!=pageSize){
			example.setOffset((pageNo.intValue()-1)*pageSize.intValue());
			example.setPageSize(pageSize.intValue());
		}
		
		List<TrainingBase> baseList =trainingBaseService.selectByExample(example);
		
		if (baseList.size()==1) {
			_resp.setTrainingBase(baseList.get(0));
		}else{
			_resp.setBaseList(baseList);
		}
		
		
		
		_resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
		_resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);
		
		return _resp;
	}

	public ITrainingBaseService getTrainingBaseService() {
		return trainingBaseService;
	}

	public void setTrainingBaseService(ITrainingBaseService trainingBaseService) {
		this.trainingBaseService = trainingBaseService;
	}

	
}
